package com.dupt.web.controller.assemble;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dupt.system.domain.param.AssembleMaterialParam;
import com.dupt.system.service.IMaterialSerialService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dupt.common.annotation.Log;
import com.dupt.common.core.controller.BaseController;
import com.dupt.common.core.domain.AjaxResult;
import com.dupt.common.enums.BusinessType;
import com.dupt.system.domain.AssembleMaterial;
import com.dupt.system.service.IAssembleMaterialService;
import com.dupt.common.utils.poi.ExcelUtil;
import com.dupt.common.core.page.TableDataInfo;

/**
 * 组装用料Controller
 * 
 * @author zhuanyu.sun
 * @date 2025-07-10
 */
@RestController
@RequestMapping("/assemble/material")
public class AssembleMaterialController extends BaseController
{
    @Autowired
    private IAssembleMaterialService assembleMaterialService;
    @Autowired
    private IMaterialSerialService materialSerialService;


    /**
     * 查询组装用料列表
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssembleMaterialParam assembleMaterialParam)
    {
        startPage();
        List<AssembleMaterial> list = assembleMaterialService.selectAssembleMaterialList(assembleMaterialParam);
        return getDataTable(list);
    }

    /**
     * 导出组装用料列表
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:export')")
    @Log(title = "组装用料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssembleMaterialParam assembleMaterialParam)
    {
        List<AssembleMaterial> list = assembleMaterialService.selectAssembleMaterialList(assembleMaterialParam);
        ExcelUtil<AssembleMaterial> util = new ExcelUtil<AssembleMaterial>(AssembleMaterial.class);
        util.exportExcel(response, list, "组装用料数据");
    }

    /**
     * 获取组装用料详细信息
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:query')")
    @GetMapping(value = "/{assembleMaterialId}")
    public AjaxResult getInfo(@PathVariable("assembleMaterialId") Long assembleMaterialId)
    {
        return success(assembleMaterialService.selectAssembleMaterialByAssembleMaterialId(assembleMaterialId));
    }

    /**
     * 新增组装用料
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:add')")
    @Log(title = "组装用料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssembleMaterial assembleMaterial)
    {
        return toAjax(assembleMaterialService.insertAssembleMaterial(assembleMaterial));
    }

    /**
     * 修改组装用料
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:edit')")
    @Log(title = "组装用料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssembleMaterial assembleMaterial)
    {
        return toAjax(assembleMaterialService.updateAssembleMaterial(assembleMaterial));
    }

    /**
     * 删除组装用料
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:receive')")
    @Log(title = "组装用料", businessType = BusinessType.OTHER)
	@DeleteMapping("/{assembleMaterialIds}")
    public AjaxResult remove(@PathVariable Long[] assembleMaterialIds)
    {
        return toAjax(assembleMaterialService.deleteAssembleMaterialByAssembleMaterialIds(assembleMaterialIds));
    }

    /**
     * 领用组装用料查询
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:remove')")
    @Log(title = "组装用料", businessType = BusinessType.OTHER)
    @GetMapping("/receiveSearch/{materialCollectId}")
    public AjaxResult receiveSearch(@PathVariable("materialCollectId") Long materialCollectId)
    {
        return success(materialSerialService.receiveSearchMaterialByMaterialCollectId(materialCollectId));
    }

    /**
     * 领用组装用料确认
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:remove')")
    @Log(title = "组装用料", businessType = BusinessType.OTHER)
    @GetMapping("/receive/{assembleMaterialId}/{receiveCount}")
    public AjaxResult receive(@PathVariable("assembleMaterialId") Long assembleMaterialId,@PathVariable("receiveCount") Long receiveCount)
    {
        return toAjax(assembleMaterialService.receiveAssembleMaterialByAssembleMaterialId(assembleMaterialId,receiveCount));
    }

    /**
     * 退还组装用料查询
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:sendBack')")
    @Log(title = "组装用料", businessType = BusinessType.OTHER)
    @DeleteMapping("/sendBackSearch/{assembleMaterialId}")
    public AjaxResult sendBackSearch(@PathVariable("assembleMaterialId") Long assembleMaterialId)
    {
        return success(materialSerialService.sendBackSearchMaterialSerialByAssembleMaterialId(assembleMaterialId));
    }

    /**
     * 退还组装用料
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:sendBack')")
    @Log(title = "组装用料", businessType = BusinessType.OTHER)
    @DeleteMapping("/sendBack/{assembleMaterialId}/{sendCount}")
    public AjaxResult sendBack(@PathVariable("assembleMaterialId") Long assembleMaterialId,@PathVariable("assembleMaterialId") Long sendCount)
    {
        return toAjax(assembleMaterialService.sendBackAssembleMaterialByAssembleMaterialId(assembleMaterialId,sendCount));
    }

    /**
     * 确认领料
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:affirm')")
    @Log(title = "组装用料", businessType = BusinessType.OTHER)
    @PostMapping("/affirm")
    public AjaxResult affirm(@RequestBody AssembleMaterialParam assembleMaterialParam)
    {
        return toAjax(assembleMaterialService.affirmAssembleMaterial(assembleMaterialParam));
    }

    /**
     * 退料
     */
    @PreAuthorize("@ss.hasPermi('assemble:material:affirm')")
    @Log(title = "组装用料", businessType = BusinessType.OTHER)
    @PostMapping("/return")
    public AjaxResult returnMaterial(@RequestBody AssembleMaterialParam assembleMaterialParam)
    {
        return toAjax(assembleMaterialService.returnAssembleMaterial(assembleMaterialParam));
    }

}
